package com.kehao.leetcode.hard.array;

import org.junit.Test;

import java.util.HashSet;

public class LongestConsecutiveSolution {
    public int longestConsecutive(int[] nums) {
        if(nums.length==0) return 0;
        int result = 1;
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            set.add(nums[i]);
        }
        for (int i = 0; i < nums.length; i++) {
            if (set.contains(nums[i]-1)){
                continue;
            }
            int candidate = 1;
            int num = nums[i];
            while (set.contains(num+1)){
                candidate ++;
                num++;
            }
            if(candidate>result){
                result = candidate;
            }
        }
        return result;
    }

    @Test
    public void test01(){
        int[] nums = {100,4,200,1,3,2};
        int result = longestConsecutive(nums);//4
        System.out.println(result);
    }

    @Test
    public void test02(){
        int[] nums = {0,3,7,2,5,8,4,6,0,1};
        int result = longestConsecutive(nums);//9
        System.out.println(result);
    }
}
